<?php

namespace App\Admin\Controllers;

use App\Models\Business;
use App\Models\BusinessCategory;
use App\Models\Role;
use Encore\Admin\Controllers\AdminController;
use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Show;
use Illuminate\Support\Facades\DB;
use Illuminate\Http\Request;

class BusinessController extends AdminController
{
    /**
     * Title for current resource.
     *
     * @var string
     */
    protected $title = '业务管理';

    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        $grid = new Grid(new Business);

        $grid->column('id', 'ID标识');
        $grid->column('business_name', '业务名称')->display(function () {
            return $this->name . '(' . $this->unit . ')';
        });

        $grid->column('data_cycle','数据周期');

        $grid->column('category_id', '所属分类')->display(function ($category_id) {
            if ($category_id == 0) {
                return '';
            }else{
                return BusinessCategory::find($category_id)->name;
            }
        });

        $grid->column('is_statistical','统计项')->using([
            true => '是',
            false => '否',
        ], '未知')->label([
            true => 'success',
            false => 'warning',
        ], 'warning');

        $grid->column('is_enable','状态')->using([
            true => '启用',
            false => '禁用',
        ], '未知')->label([
            true => 'success',
            false => 'warning',
        ], 'warning');


        $grid->column('created_at', '创建时间')->date('Y-m-d');

        return $grid;
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     * @return Show
     */
    protected function detail($id)
    {
        $show = new Show(Business::findOrFail($id));



        return $show;
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        $form = new Form(new Business);

        $form->display('id', 'ID');
        $form->text('name', '业务名称');
        $form->textarea('description', '业务描述');
        $form->radio('is_statistical','是否统计项')->options([ 1 => '是',0 => '否'])->default(0);
        $form->select('parent_id', '父类名称')->options(Business::selectOptions());
        $form->number('order', '排序序号')->default(1);
        $form->text('unit', '单位');
        $form->select('category_id', '分类名称')->options(BusinessCategory::selectOptions());
        $form->radio('is_enable','是否启用')->options([ 1 => '是',0 => '否'])->default(1);
        $form->select('data_cycle','周期')->options(['月' => '月', '季度' => '季度', '半年' => '半年','年'=>'年']);
        $form->select('role_id','关联角色')->options(Role::selectBusinessRoleOptions());

        return $form;
    }

    public function getBusiness(Request $request)
    {
        return DB::table('businesses')->get(['id', DB::raw('name as text')]);
    }

}
